﻿using Controls.Message;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Controls.Views
{
    public partial class MessBox : Form
    {
        //private Color _okYesBtnColor;
        //private Color _noBtnColor;
        //private Color _cancelBtnColor;

        private MessBox(Form form,string message)
        {
            SetBase();
            SetInfo(message);
            if(form != null && form.IsHandleCreated)
            {
                Owner = form;
            }
        }

        private MessBox(string message)
        {
            SetBase();
            SetInfo(message);
            var enumerator = Application.OpenForms.GetEnumerator();
            Form form = null;
            if (enumerator.MoveNext())
            {
                form = enumerator.Current as Form;
            }

            if (form != null && form.IsHandleCreated)
            {
                Owner = form;
            }
        }

        public static DialogResult Show(string message)
        {
            MessBox messBox = new MessBox(message);
            SetTypeOfMessBox(messBox, MessageBoxButtons.OK, MessageBoxIcon.None);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(Form form,string message)
        {
            MessBox messBox = new MessBox(form, message);
            SetTypeOfMessBox(messBox, MessageBoxButtons.OK, MessageBoxIcon.None);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(Form form,string message,MessageBoxButtons messageBoxButtons)
        {
            MessBox messBox = new MessBox(form, message);
            SetTypeOfMessBox(messBox, messageBoxButtons, MessageBoxIcon.None);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(Form form,string message,MessageBoxButtons messageBoxButtons,MessageBoxIcon messageBoxIcon)
        {
            MessBox messBox= new MessBox(form, message);
            SetTypeOfMessBox(messBox, messageBoxButtons, messageBoxIcon);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(string message, MessageBoxButtons messageBoxButtons)
        {
            MessBox messBox = new MessBox(message);
            SetTypeOfMessBox(messBox, messageBoxButtons,MessageBoxIcon.None);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(string message,string title ,MessageBoxButtons messageBoxButtons)
        {
            MessBox messBox = new MessBox(message)
            {
                Text = title,
            };
            SetTypeOfMessBox(messBox, messageBoxButtons, MessageBoxIcon.None);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(string message,string title,MessageBoxButtons messageBoxButtons, MessageBoxIcon messageBoxIcon)
        {
            MessBox messBox = new MessBox(message)
            {
                Text = title
            };
            SetTypeOfMessBox(messBox, messageBoxButtons, messageBoxIcon);
            return messBox.OpenDialog();
        }

        public static DialogResult Show(string message,MessageBoxButtons messageBoxButtons,MessageBoxIcon messageBoxIcon)
        {
            MessBox messBox = new MessBox(message);
            SetTypeOfMessBox(messBox, messageBoxButtons, messageBoxIcon);
            return messBox.OpenDialog();
        }

        //public static DialogResult Show(string message,MessageBoxButtons messageBoxButtons,MessageBoxIcon messageBoxIcon,Color okOrYesBtnColor)
        //{
        //    MessBox messBox = new MessBox(message);

        //}

        private DialogResult OpenDialog()
        {
            int len = this.label_Message.Text?.Length ?? 0;
            if (len > 60)
            {
                this.Size = new Size(Width + 30, Height + 12);                
            }
            else if(len > 30)
            {
                this.Size = new Size(Width + 15, Height + 6);
            }

            if (Owner != null)
            {
                 return (DialogResult)Owner.Invoke((Func<DialogResult>)(()=> 
                 { 
                     return this.ShowDialog(); 
                 }));
            }
            else
            {
                return this.ShowDialog();
            }
        }

        private void SetBase()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void SetInfo(string message)
        {
            this.label_Message.Text = message;
        }

        private static void SetTypeOfMessBox(MessBox messBox,MessageBoxButtons messageBoxButtons,MessageBoxIcon messageBoxIcon)
        {
            switch (messageBoxButtons)
            {
                case MessageBoxButtons.OK:
                    messBox.SetOK();
                    break;
                case MessageBoxButtons.YesNoCancel:
                    messBox.SetYesNoCancel();
                    break;
                case MessageBoxButtons.OKCancel:
                    messBox.SetOkCancel();
                    break;
                default:
                    messBox.SetOK();
                    break;
            }

            switch (messageBoxIcon)
            {
                case MessageBoxIcon.None:
                    DrawNone(messBox);
                    break;
                case MessageBoxIcon.Warning:
                    DrawWarning(messBox);
                    break;
                case MessageBoxIcon.Error:
                    DrawError(messBox);
                    break;
                case MessageBoxIcon.Information:
                    DrawInfo(messBox);
                    break;
                default:
                    DrawNone(messBox);
                    break;
            }
        }

        private void SetOK(string okBtnText = null)
        {
            PButton okBtn = new PButton
            {
                Size = new Size(70, 26),
                Location = new Point((panel_Button.Width - 70) / 2, 3),
                Text = okBtnText ?? "确定"
            };
            okBtn.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            };
            panel_Button.Controls.Add(okBtn);
        }

        private void SetOkCancel(string okBtnText = "确定",string cancelBtnText = "取消")
        {
            int interval = 12;
            PButton okBtn = new PButton
            {
                Size = new Size(70, 26),
                Location = new Point((panel_Button.Width - 70 * 2 - interval) / 2, 3),
                Text = okBtnText
            };
            okBtn.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            };

            PButton cancelBtn = new PButton
            {
                Size = new Size(70, 26),
                Location = new Point((okBtn.Location.X + okBtn.Width + interval), 3),
                Text = cancelBtnText
            };
            cancelBtn.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            };
            panel_Button.Controls.Add(okBtn);
            panel_Button.Controls.Add(cancelBtn);
        }

        private void SetYesNoCancel(string yesBtnText = "是",string noBtnText = "否",string cancelBtnText = "取消")
        {
            int interval = 12;

            PButton yesBtn = new PButton
            {
                Size = new Size(70, 26),
                Location = new Point((panel_Button.Width - 70 * 3 - 2 * interval) / 2, 3),
                Text = yesBtnText
            };
            yesBtn.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.Yes;
                this.Close();
            };

            PButton noBtn = new PButton
            {
                Size = new Size(70, 26),
                Location = new Point(yesBtn.Location.X + yesBtn.Width + interval, 3),
                Text = noBtnText
            };
            noBtn.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.No;
                this.Close();
            };

            PButton cancelBtn = new PButton
            {
                BorderColor = Color.Gray,
                MouseOverBorderColor = Color.DarkGray,
                Size = new Size(70, 26),
                Location = new Point(noBtn.Location.X + noBtn.Width + interval, 3),
                Text = cancelBtnText
            };
            cancelBtn.Click += (s, e) =>
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            };

            panel_Button.Controls.Add(yesBtn);
            panel_Button.Controls.Add(noBtn);
            panel_Button.Controls.Add(cancelBtn);
        }

        private static void DrawNone(MessBox messBox)
        {
            messBox.label_Message.TextAlign = ContentAlignment.TopLeft;
            messBox.label_Message.Left = (messBox.Width - messBox.label_Message.Width) / 2;
            messBox.label_Message.Top = (messBox.panel_Button.Top - messBox.label_Message.Height) / 2;
        }

        private static void DrawWarning(MessBox messBox)
        {
            WarningIcon warningControl = new WarningIcon
            {
                Size = new Size(36, 29),
                Location = new Point((messBox.Width - messBox.label_Message.Width - 36 - 15) / 2, (messBox.panel_Button.Top - 36) / 2)
            };
            messBox.Controls.Add(warningControl);

            messBox.label_Message.TextAlign = ContentAlignment.TopLeft;
            messBox.label_Message.Left = warningControl.Left + warningControl.Width + 15;
            messBox.label_Message.Top = (messBox.panel_Button.Top - messBox.label_Message.Height) / 2;
        }

        private static void DrawInfo(MessBox messBox)
        {
            InfoIcon infoControl = new InfoIcon
            {
                Size = new Size(34, 34),
                Location = new Point((messBox.Width - messBox.label_Message.Width - 32 - 12) / 2, (messBox.panel_Button.Top - 32) / 2)
            };

            messBox.Controls.Add(infoControl);

            messBox.label_Message.TextAlign = ContentAlignment.TopLeft;
            messBox.label_Message.Left = infoControl.Left + infoControl.Width + 12;
            messBox.label_Message.Top = (messBox.panel_Button.Top - messBox.label_Message.Height) / 2;
        }

        private static void DrawError(MessBox messBox)
        {
            ErrorIcon errorControl = new ErrorIcon
            {
                Size = new Size(32, 32),
                Location = new Point((messBox.Width - messBox.label_Message.Width - 32 - 12) / 2, (messBox.panel_Button.Top - 32) / 2)
            };
            messBox.Controls.Add(errorControl);

            messBox.label_Message.TextAlign = ContentAlignment.TopLeft;
            messBox.label_Message.Left = errorControl.Left + errorControl.Width + 12;
            messBox.label_Message.Top = (messBox.panel_Button.Top - messBox.label_Message.Height) / 2;
        }

        //#region 顶上标题栏（用Panel模拟的标题栏）实现拖拽效果
        //private Point _mousePoint = new Point();
        //
        //private void PanelTop_MouseDown(object sender, MouseEventArgs e)
        //{
        //    base.OnMouseDown(e);
        //    this._mousePoint.X = e.X;
        //    this._mousePoint.Y = e.Y;
        //}

        //private void Panel_Top_MouseMove(object sender, MouseEventArgs e)
        //{
        //    base.OnMouseMove(e);
        //    if (e.Button == MouseButtons.Left)
        //    {
        //        this.Top = Control.MousePosition.Y - _mousePoint.Y;
        //        this.Left = Control.MousePosition.X - _mousePoint.X;
        //    }
        //}
        //#endregion
    }
}
